package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import com.sky.vo.Turnover;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {

    /**
     * 插入订单
     * @param orders
     */
    void insert(Orders orders);

    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 根据订单状态和时间查询订单
     * @param status
     * @param time
     * @return
     */
    @Select("select * from orders where status =#{status} and order_time<#{time};")
    List<Orders> getOrderByStatusAndTime(Integer status, LocalDateTime time);

    /**
     * 分页条件查询并按下单时间排序
     * @param ordersPageQueryDTO
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据id查询订单
     * @param id
     */
    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);

    /**
     * 根据状态统计订单数量
     * @param status
     */
    @Select("select count(id) from orders where status = #{status}")
    Integer countStatus(Integer status);

    /**
     * 根据订单状态和下单时间查询订单
     * @param status
     * @param orderTime
     * @return
     */
    @Select("select * from orders where status = #{status} and order_time < #{orderTime}")
    List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime orderTime);

    /**
     * 根据动态条件统计营业额数据
     * @param map
     * @return
     */
    Double sumByMap(Map map);

    /**
     * 根据动态条件统计订单数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);


    /**
     * 分页查询pageQueryOrderVOList
     * @param ordersPageQueryDTO
     * @return
     */
    Page<OrderVO> pageQueryOrderVOList(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据订单id查询订单详情
     * @param id
     */
    List<OrderDetail> selectOrderDetailByOrderId(Long id);

    /**
     * 查询订单详情
     *
     * @param id
     * @return
     */
    OrderVO getQueryOrderVO(Long id);

    /**
     * 各个状态的订单数量统计
     *
     * @return
     */
    @Select("select count(status = 2 or null) toBeConfirmed," +
            "       count(status = 3 or null) confirmed," +
            "       count(status = 4 or null) deliveryInProgress " +
            "from orders")
    OrderStatisticsVO countOrderStatisticsVO();

    /**
     * 查询订单根据状态和下单时间
     * @param status
     * @param time
     * @return
     */
    @Select("select * from orders where status = #{status} and order_time  <= #{time}")
    List<Orders> getOrderByStatusAndOrderTime(Integer status, LocalDateTime time);

    /**
     * 批量修改
     * @param ordersList
     */
    void updateBatch(List<Orders> ordersList);

    /**
     * 统计营业额
     * @param map
     * @return
     */
    Double countTurnover(Map map);

    /**
     * 统计营业额
     * @param map
     * @return
     */
    List<Turnover> countTurnoverList(Map map);

    /**
     * 统计订单数
     * @param map
     * @return
     */
    Integer countOrders(Map map);

    List<GoodsSalesDTO> top10(LocalDate begin,LocalDate end);
}
